/******************************** ************************ *********************************/
/********************************       时间工具类         *********************************/
/******************************** ************************ *********************************/

import React, {Component} from 'react';

class TimeUtil extends Component {
    /**
     * 获取当前完整日期，格式：2019-04-02 05:15:00
     */
    static getCurrentFullDate() {
        let date = new Date();
        let year = date.getFullYear().toString();
        let month = (date.getMonth() + 1).toString();
        let day = date.getDate().toString();
        let hour = date.getHours() + 8;
        let minute = date.getMinutes();
        let second = date.getSeconds();

        if (hour < 10) {
            hour = "0" + hour;
        }
        if (minute < 10) {
            minute = "0" + minute;
        }
        if (second < 10) {
            second = "0" + second;
        }

        console.log("getCurrentFullDate()：" + year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second);
        let result = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
        return result;
    }

    /**
     * 获取当前日期，格式：2019-04-02
     */
    static getCurrentDate() {
        let date = new Date();
        let year = date.getFullYear().toString();
        let month = (date.getMonth() + 1).toString();
        let day = date.getDate().toString();

        console.log("getCurrentDate()：" + year + "-" + month + "-" + day);
        return year + "-" + month + "-" + day;
    }

    /**
     * 获取当前时间，格式：05:15:00
     */
    static getCurrentTime() {
        let date = new Date();
        let hour = date.getHours() + 8;
        let minute = date.getMinutes();
        let second = date.getSeconds();

        if (hour < 10) {
            hour = "0" + hour;
        }
        if (minute < 10) {
            minute = "0" + minute;
        }
        if (second < 10) {
            second = "0" + second;
        }

        console.log("getCurrentTime()：" + hour + ":" + minute + ":" + second);
        return hour + ":" + minute + ":" + second;
    }

    /**
     * 比较时间，结果为秒
     */
    static compareTime(startTime, endTime) {
        let time1 = transForm(startTime);
        let time2 = transForm(endTime);
        let date3 = new Date(time2).getTime() - new Date(time1).getTime();   //时间差的毫秒数
        console.log("相差：" + date3 / 1000 + " 秒");
    }

    static add0(m) {
        return m < 10 ? '0' + m : m
    }

    /**
     * 根据时间戳获取字符串，格式：2019-04-02 05:15:00
     * timestamp ： new Date()需要毫秒级时间戳
     */
    static getTimeFromTimesTamp(timestamp) {
        let time1 = null;
        if (timestamp.length > 10) {
            time1 = new Date(parseFloat(timestamp));
        } else {
            time1 = new Date(parseFloat(timestamp + "000"));
        }
        let y = time1.getFullYear();
        let m = time1.getMonth() + 1;
        let d = time1.getDate();
        let h = time1.getHours();
        let mm = time1.getMinutes();
        let s = time1.getSeconds();
        let result = y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
        console.log("时间戳转为字符串：" + result);

        //console.log("年:"+(1970+years)+"天:"+days+",时:"+hours+",分:"+minutes+",秒:"+seconds);
        //console.log("compareTime()："+year+","+month+","+day+","+hour+","+minute+","+second);
    }

    /*
    * 转化时间格式
    * 2019-04-02 14:22:22  转化为  2019/04/02 14:22:22
    * 方便比较时间时Date()函数的使用
    */
    static transForm(stringTime) {

        let year = stringTime.substring(0, 4);
        let month = stringTime.substring(5, 7);
        let day = stringTime.substring(8, 10);
        let hour = stringTime.substring(11, 13);
        let minute = stringTime.substring(14, 16);
        let second = stringTime.substring(17, 19);

        return year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + second;

    }

}


export default TimeUtil;














